(function ($) {
    $.fn.extend({
        lytebox: function (options) {
            
            var defaults = {
                selector: '',
                show: false,
                showClose: true,
                closeButton: null
            };
            var options = $.extend(defaults, options);
            var lb = $(this);
            if (lb.length == 0) return;
            var lblength = $('.lytebox-overlay').length;
            //            if ($('#lytebox-overlay').length == 0) {
            $('body').append('<div class="lytebox-overlay" style="background-color:#777;cursor: pointer;"></div>');
            //}
            //if ($('#lytebox-wrap').length == 0) {
            var html = '<div class="lytebox-wrap">';
            html += '<div class="lytebox-outer">';
            html += '<div class="lytebox-bg lytebox-bg-n"></div>' +
                '<div class="lytebox-bg lytebox-bg-ne"></div>' +
                '<div class="lytebox-bg lytebox-bg-e"></div>' +
                '<div class="lytebox-bg lytebox-bg-se"></div>' +
                '<div class="lytebox-bg lytebox-bg-s"></div>' +
                '<div class="lytebox-bg lytebox-bg-sw"></div>' +
                '<div class="lytebox-bg lytebox-bg-w"></div>' +
                '<div class="lytebox-bg lytebox-bg-nw"></div>';
            html += '<div class="lytebox-content"></div>';
            if (options.showClose) {
                html += '<div class="lytebox-close"><a href="close"></a></div>';
            }
            html += '</div>';
            html += '</div>';
            //}
            $('body').append(html);
            lb.orderIdx = lblength;
            lb.offsetX = 0;
            lb.offsetY = 0;
            lb.close = function () {
                var w = $(window).width();
                var h = $(window).height();
                var st = $(document).scrollTop();
                lb.overlay.stop().animate({ opacity: 0 }, 400, function () {
                    $(this).css('display', 'none');
                });
                lb.wrap.stop().animate({ width: 0, height: 0, left: lb.offsetX != 0 ? lb.offsetX : w / 2, top: lb.offsetY != 0 ? lb.offsetY : h / 2 + st }, 400, function () {
                    $(this).css('display', 'none');
                });
            };
            lb.show = function (o) {
                var d = {
                    reload: false
                };
                var o = $.extend(d, o);
                if (o.reload) {
                    lb.setContent(e);
                }
                lb.offsetX = o.offsetX ? o.offsetX : lb.offsetX;
                lb.offsetY = o.offsetY ? o.offsetY : lb.offsetY;
                var w = $(window).width();
                var h = $(window).height();
                var st = $(document).scrollTop();
                lb.overlay.css({ height: $(document).height(), display: 'block', opacity: 0 });
                var cw = lb.content.width();
                var ch = lb.content.height();
                lb.wrap.css({ width: 0, height: 0, left: o.offsetX ? o.offsetX : w / 2, top: o.offsetY ? o.offsetY : h / 2 + st, display: 'block' });
                lb.overlay.animate({ opacity: 0.7 }, 400, function () {
                    lb.overlay.css({ height: $(document).height() });
                });
                lb.wrap.animate({ width: cw, height: ch, left: (w - cw) / 2, top: ch < h ? (h - ch) / 2 + st : st }, 400);
            };
            lb.setContent = function (e) {
                
                lb.content.html('');
                var w = e.width();
                var h = e.height();
                e.remove().css('display', 'block').appendTo(lb.content);
                lb.content.css({ width:w, height: h });
            };
            lb.wrap = $('.lytebox-wrap:eq(' + lb.orderIdx + ')');
            lb.content = $('.lytebox-content:eq(' + lb.orderIdx + ')');
            lb.overlay = $('.lytebox-overlay:eq(' + lb.orderIdx + ')');
            if (options.selector == '') {
                if (lb.prop('tagName').toLowerCase() == 'a') {
                    lb.setContent($($(this).attr('href')));
                }
                else {
                    lb.setContent(lb);
                }
            }
            else {
                lb.setContent($(options.selector));
            }
            lb.closeButton = $('.lytebox-close:eq(' + lb.orderIdx + ') a');
            lb.closeButton.click(function (e) {
                e.preventDefault();
                lb.close();
            });
            if (options.closeButton != null && options.closeButton.length > 0) {
                options.closeButton.click(function (e) {
                    e.preventDefault();
                    lb.close();
                });
            }
            lb.overlay.click(function () {
                lb.close();
            });
            if (lb.prop('tagName').toLowerCase() == 'a') {
                lb.click(function (e) {
                    e.preventDefault();
                    lb.show();
                });
            }
            if (options.show) {
                lb.click();
            }
            return lb;
        }
    });
})(jQuery);